/*
 * $Id: Preference.java,v 1.2 2001/10/13 16:19:25 jppietsch Exp $
 * Copyright (C) 2001 Jan-Peter Pietsch
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package de.jppietsch.prefedit;


import de.jppietsch.error.Precondition;

import java.util.prefs.Preferences;


/**
 * An immutable key/value pair which may be placed in the clipboard.
 *
 * @author Jan-Peter Pietsch
 * @version $Revision: 1.2 $
 */
public class Preference extends AbstractTransferable {

    // constants

    // fields

    /**
     * The preferences node.
     */
    private Preferences prefs;

    /**
     * The key.
     */
    private final String key;

    /**
     * The value.
     */
    private final String value;

    // constructors

    /**
     * Constructs this Preference from a given preferences node and key.
     *
     * @param somePreferences the preferences node.
     * @param aKey            the value's key.
     */
    public Preference(Preferences somePreferences, String aKey) {
        Precondition.argumentNotNull("somePreferences", somePreferences);
        Precondition.argumentStringNotEmpty("aKey", aKey);
        prefs = somePreferences;
        key = aKey;
        value = prefs.get(aKey, null);
    }

    // overwritten methods

    /**
     * Returns a string representation of this object.
     *
     * @return A string representation of this object.
     */
    public String toString() {
        return key + " = \"" + value + "\"";
    }

    /**
     * Copies this preference to the system clipboard and removes it from the
     * preferences node.
     */
    public void cut() {
        super.cut();
        prefs.remove(key);
        prefs = null;
    }

    // implemented methods

    /**
     * Returns a human presentable name for this class.
     *
     * @return A human presentable name for the concrete class.
     */
    protected String humanPresentableClassName() {
        return "Preference (key/value pair)";


    }

    /**
     * Adds this transferable to the given preferences.
     *
     * @param somePreferences to which this is added.
     */
    protected void doPaste(Preferences somePreferences) {
        somePreferences.put(key, value);
    }

}